<?php
namespace common\components\helpers;

use EasyWeChat\Support\Arr;
use yii\base\BaseObject;
use yii\helpers\ArrayHelper;

class MsgeventData extends BaseObject{

    private $_config;

    private $_configSendGroup;

    public $className;

    public $classOwner;

    public $eventName;

    public $handler;

    private $_event_relate_group;

    public function init(){
        $this->_config=\Yii::$app->params['msg_event'];
        $this->_configSendGroup=\Yii::$app->params['receive_group'];
        $this->_event_relate_group = \Yii::$app->params['event_relate_group'];
        parent::init();
    }

    public function getEventArray()
    {
        $cache = $this->_config;
        $eventarray = [];
        foreach ($cache as $k => $v) {
            if ($v['model'] == $this->classOwner && $v['behavior']== $this->className){
                if(!empty($v['event'])&&!empty($v['handler'])) {
                    $eventarray[$v['event']] = $v['handler'];//针对模型的事件的处理，key是模型的事件名，hander是行为类的处理方法；
                }
            }
        }
        return $eventarray;
    }

    public function getBehaviorArray()
    {
        $behaviers_cache = $this->_config;
        $behaviorarray = [];
        $existed_behavior_arr = [];
        foreach ($behaviers_cache as $k => $v) {
            if ($v['model'] == $this->className) {
                if(in_array($v['behavior'],$existed_behavior_arr)){
                    continue;
                }else{
                    $class['class'] = $v['behavior'];
                    $behaviorarray[] = isset($v['config'])&&is_array($v['config'])?array_merge($class, $v['config']):$class;
                    $existed_behavior_arr[] = $v['behavior'];
                }
            }
        }
        return $behaviorarray;
    }

    public function getEventKey()
    {
        $cache = $this->_config;
        foreach ($cache as $k => $v) {
            if ($v['model'] == $this->classOwner && $v['behavior']== $this->className && $v['event'] == $this->eventName && $v['handler'] == $this->handler){
                return $k;
            }
        }
        return 0;
    }

    public function getEventKeys(){
        $cache = $this->_config;
        $result = [];
        foreach ($cache as $k => $v) {
            if ($v['model'] == $this->classOwner && $v['behavior']== $this->className && $v['event'] == $this->eventName && $v['handler'] == $this->handler){
                $result[] = $k;
            }
        }
        return $result;
    }

    public function getEventbyKey($key){
        return isset($this->_config[$key])?$this->_config[$key]:null;
    }
    public function getTemplatebyKey($key){
        return isset($this->_config[$key]['template'])?$this->_config[$key]['template']:'不存在';
    }

    public function getReceiveGroupByKey($key){
        return isset($this->_configSendGroup[$key])?$this->_configSendGroup[$key]:'';
    }

    public function getReceivegrouparr(){
        $configsendgroup = $this->_configSendGroup;
        foreach ($configsendgroup as $k=>$value){
            if(isset($value['type']) && $value['type'] == 'hide'){
                unset($configsendgroup[$k]);
            }
        }
        return ArrayHelper::getColumn($configsendgroup,'name',true);
    }

    public function getReceivegroupkeys(){
        $configsendgroup = $this->_configSendGroup;
        foreach ($configsendgroup as $k=>$value){
            if(isset($value['type']) && $value['type'] == 'class'){
                unset($configsendgroup[$k]);
            }
            if(isset($value['type']) && $value['type'] == 'hide'){
                unset($configsendgroup[$k]);
            }
        }
        return array_keys($configsendgroup);
    }
    /*
     * 与班级相关的接收者类型
     */
    public function getReceivegroupkeysforClass(){
        $configsendgroup = $this->_configSendGroup;
        foreach ($configsendgroup as $k=>$value){
            if(!(isset($value['type']) && $value['type'] == 'class')){
                unset($configsendgroup[$k]);
            }
        }
        return array_keys($configsendgroup);
    }

    public function getExtraDeclare($key){
        return isset($this->_config[$key]['extra_declare'])?$this->_config[$key]['extra_declare']:null;
    }

    public function getNameByKey($key){
        return isset($this->_config[$key]['name'])?$this->_config[$key]['name']:'不存在';
    }

    public function getNameList(){
        return ArrayHelper::getColumn($this->_config,'name',true);
    }

    public function getEventRelatedGroup($event_key){
        return $this->_event_relate_group[$event_key];
    }



}